package com.mtk.launcher.util;


import com.mtk.launcher.view.LayoutViewBase;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;

public class TimerDealUtil {

    public static TimerDealUtil instance;
    Disposable mDisposable;
    List<LayoutViewBase> list = new ArrayList<>();

    public static TimerDealUtil getInstance() {
        if (instance == null) {
            synchronized (TimerDealUtil.class) {
                if (instance == null) {
                    instance = new TimerDealUtil();
                }
            }
        }
        return instance;
    }

    public void startToTimerMinRxJava() {
        log("=startToTimerMinRxJava=");
        Observable.interval(1000, TimeUnit.MILLISECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable disposable) {
                        mDisposable = disposable;
                    }

                    @Override
                    public void onNext(@NonNull Long number) {
                        notifyAllGeneratorInfo();
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mDisposable = null;
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void notifyAllGeneratorInfo() {
        log("=notifyAllGeneratorInfo=000");
        if (mDisposable == null) {
            log("=notifyAllGeneratorInfo=000=mDisposable == null");
            return;
        }
        if (list == null || list.size() < 1) {
            log("=notifyAllGeneratorInfo=000=list.size() == null");
            return;
        }
        for (LayoutViewBase generator : list) {
            generator.timeChangeToUpdateView();
        }
    }

    private void log(String s) {
//        MyLog.cdl("time=" + s);
    }

    public void addGeneratorToList(LayoutViewBase generator) {
        if (generator == null) {
            return;
        }
        log("=addGeneratorToList=" + generator);
        if (list == null) {
            list = new ArrayList<LayoutViewBase>();
        }
        list.add(generator);
    }


    public void removeGeneratorToList(LayoutViewBase generator) {
        if (generator == null) {
            return;
        }
        log("=removeGeneratorToList=" + generator);
        if (list == null || list.size() < 1) {
            return;
        }
        if (list.contains(generator)) {
            list.remove(generator);
        }
    }

    public void onDestroyTimer() {
        log("=onDestroyTimer=");
        if (mDisposable != null && !mDisposable.isDisposed()) {
            mDisposable.dispose();
        }
        removeAllGenerator();
    }

    private void removeAllGenerator() {
        log("=removeAllGenerator=");
        if (list == null || list.size() < 1) {
            return;
        }
        list.clear();
    }
}
